<?php
/**
 * Created by PhpStorm.
 * User: von
 * Date: 2017/11/29
 * Time: 11:08
 */
namespace common\helps;
/**
 * 分页工具类
 */
class pageTools{

    const FIRST_0PAGE = 0; //第一页
    const FIRST_PAGE = 0; //第一页
    const OFFSET_PAGE = 3; //偏移量


    /**
     * 列表页分页样式方法
     * @param string|array $url 当前的链接
     * @param int $total 全部页数
     * @param int $page 当前的页数
     * @param int $size 分页
     * @param int $offset 当前页前后显示的个数
     * @return string
     */
    public static function createPageString ($url, $total, $page, $size = 10, $offset = 2) {

        $pageString = '';
        $pageCount = intval(ceil($total/$size))-1; //分页的总数
        if ($total<= $size) {
            return $pageString; //如果信息不够分页则直接返回
        }

        if($page < $pageCount) {
            $pageString .= self::getPage(self::conditionHandling($url, $page+1), '下一页');
        }

        //当前页为第一页时
        //if($page == self::FIRST_PAGE) {
        //$pageString .= self::getPage(self::conditionHandling($url, $pageCount), '末页');
        //} else {
        if($page <= $pageCount-self::OFFSET_PAGE) {
            $pageString .= self::getPage(self::conditionHandling($url, $pageCount), $pageCount);
            if($page < $pageCount-self::OFFSET_PAGE) {
                $pageString .= self::getPage(self::conditionHandling($url, self::FIRST_PAGE), '...', false);
            }
        }
        //}

        $loopPageMax = ($page+$offset < $pageCount) ? $page + $offset : $pageCount; //展示当前页码上限
        $loopPageMin = (($page - $offset)>$offset) ? $page-$offset : self::FIRST_PAGE; //展示当前页码下限
        for ($i = $loopPageMax; $i >= $loopPageMin; $i--) {
            $current = ($page == $i) ? false : true;
            $pageString .= self::getPage(self::conditionHandling($url, $i), $i, $current, $current);
        }

        //当前页为最后一页时
        if($page == $pageCount) {
            $pageString .= self::getPage(self::conditionHandling($url, self::FIRST_PAGE), '第一页');
        } else {
            if($page >= 5) {
                $pageString .= self::getPage(self::conditionHandling($url, self::FIRST_PAGE), '...', false);
                $pageString .= self::getPage(self::conditionHandling($url, self::FIRST_PAGE), self::FIRST_PAGE);
            }
        }

        //除了首页外均显示上一页
        if($page > self::FIRST_PAGE) {
            $pageString .= self::getPage(self::conditionHandling($url, $page-1), '上一页');
        }

        return self::mergePage($pageString);
    }

    /**
     * 列表页分页样式方法
     * @param string|array $url 当前的链接
     * @param int $total 全部页数
     * @param int $page 当前的页数
     * @param int $size 分页
     * @param int $offset 当前页前后显示的个数
     * @return string
     */
    public static function createAjaxPageString ($url, $total, $page, $size = 10, $offset = 2) {

        $pageString = '';
        $pageCount = intval(ceil($total/$size)); //分页的总数
        if ($total<= $size) {
            return $pageString; //如果文章不够分页则直接返回
        }

        if($page < $pageCount) {
            $pageString .= self::getPage(self::conditionHandling($url, $page+1), '下一页', false);
        }

        //当前页为第一页时
        if($page == self::FIRST_PAGE) {
            $pageString .= self::getPage(self::conditionHandling($url, $pageCount), '末页', false);
        } else {
            if($page <= $pageCount-self::OFFSET_PAGE) {
                $pageString .= self::getPage(self::conditionHandling($url, $pageCount), $pageCount, false);
                if($page < $pageCount-self::OFFSET_PAGE) {
                    $pageString .= self::getPage(self::conditionHandling($url, self::FIRST_PAGE), '...', false);
                }
            }
        }

        $loopPageMax = ($page+$offset < $pageCount) ? $page + $offset : $pageCount; //展示当前页码上限
        $loopPageMin = (($page - $offset)>$offset) ? $page-$offset : self::FIRST_PAGE; //展示当前页码下限
        for ($i = $loopPageMax; $i >= $loopPageMin; $i--) {
            $current = ($page == $i) ? false : true;
            $pageString .= self::getPage(self::conditionHandling($url, $i), $i, false, $current);
        }

        //当前页为最后一页时
        if($page == $pageCount) {
            $pageString .= self::getPage(self::conditionHandling($url, self::FIRST_PAGE), '第一页', false);
        } else {
            if($page >= 5) {
                $pageString .= self::getPage(self::conditionHandling($url, self::FIRST_PAGE), '...', false);
                $pageString .= self::getPage(self::conditionHandling($url, self::FIRST_PAGE), self::FIRST_PAGE, false);
            }
        }

        //除了首页外均显示上一页
        if($page > self::FIRST_PAGE) {
            $pageString .= self::getPage(self::conditionHandling($url, $page-1), '上一页', false);
        }

        return self::mergePage($pageString);
    }


    /**
     * 分页
     */
    public static function getPage($url, $name = '', $jump = true, $current = true) {
        $current = !$current ? 'cur' : '';
        if(is_numeric($name) || $name == '...') {
            $className = $current;
        }else {
            $className = 'cPage'. $current;
        }
        $url = $jump ? $url : 'javascript:void(0);';
        if (is_numeric($name)){
            $nameNum = $name+1;
        }else{
            $nameNum = $name;
        }
        return '<a href="'. $url .'" class="'. $className .'">'. $nameNum .'</a>';
    }

    /**
     * 组合分页HTML
     */
    public static function mergePage($pageString) {
        $string = '<div class="pageSty rf">';
        $string .= $pageString;
        $string .= '</div>';
        return $pageString;
    }

    /**
     * 组合分页URL
     */
    public static function conditionHandling($url, $page='') {
        if(!is_array($url)) {
            return $url;
        }
        if(isset($url['condition'])) {
            if(!empty($page) && $page > self::FIRST_0PAGE) {
                $url['condition'] = preg_replace('/n[1-9][0-9]*/', '', $url['condition']).'&from='.$page;
            } else {
                $url['condition'] = preg_replace('/n[1-9][0-9]*/', '', $url['condition']);
            }
        }
        if(!empty($url['keywords'])) {
            $url['keywords'] = '_'. $url['keywords'];
        }
        foreach($url as $k => $v) {
            if(empty($v)) {
                unset($url[$k]);
            }
        }
        $url = '/'.implode('/', array_filter($url));
        return $url;
    }




}
